<?php
/**
 * @auther:周卫星
 * @className:IndexController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/10/31
 * @time:10:02
 */

namespace App\Http\Controllers\biz;


use App\Http\Controllers\Controller;
use App\Model\MallOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class IndexController extends Controller
{
    protected $mall_order;
    public function __construct()
    {
        $this -> mall_order = new MallOrder();
    }

    /**
     * 订单信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request -> isMethod('get') && !$request -> ajax()){
//            View::share('a','b');
            return view("biz.index.index");
        }
        if ($request -> isMethod('get') && $request -> ajax()){
            // 获取订单的状态
            $order_state = $request -> get('state', '');
            // 订单号
            $order_sn = $request -> get('order_sn', '');
            // 获取餐台
            $table_sn = $request -> get('table_sn', '');
            // 获取起始时间
            $start_time = $request -> get('start_time', '');
            $end_time = $request -> get('end_time', '');
            $between = [];
            if (empty($order_state)){
                $order_state = [1,2,3,4,5];
            }else{
                $order_state = [$order_state];
            }
            if (!empty($start_time)){
                $between = [strtotime($start_time." 00:00:00"), strtotime($end_time." 23:59:59")];
            }else{
                $between = [strtotime(date("Y-m-d", time())." 00:00:00"), strtotime(date("Y-m-d", time())." 23:59:59")];
            }
            $list = $this -> mall_order -> getOrderList($order_sn, $table_sn, $between, $order_state);
            if ($list){
                if ($list['total'] !== 0){
                    $result = [
                        'code' => 0,
                        'message' => '获取成功',
                        'count' => $list['total'],
                        'data' => $list['data']
                    ];
                }else{
                    $result = [
                        'code' => 0,
                        'message' => '暂无订单列表',
                        'count' => 0,
                        'data' => []
                    ];
                }
            }else{
                $result = [
                    'code' => 1,
                    'message' => '订单获取失败',
                    'count' => 0,
                    'data' => []
                ];
            }
            return  $this -> returnJson($result);
        }
    }
}
